package com.wenshi.app.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.wenshi.view.IKeyValue;
import com.wenshi.view.IWsView;
import com.wenshi.view.WsVIewClickListener;
import com.wenshi.view.WsViewTools;

import java.util.ArrayList;

import java.util.List;



/**
 * Created by mrren on 2017/3/1.
 */
public class WsViewAdapter extends BaseAdapter {
    /**
     * The Context.
     */
    Context context;
    private LayoutInflater mInflater;
    private int lay_out_id = 0;
    private WsVIewClickListener clickListener;

    /**
     * The interface Ws view get layout id.
     */
    public interface IWsViewGetLayoutId {
        /**
         * Gets layout id.
         *
         * @param item    the item
         * @param postion the postion
         * @return the layout id
         */
        int getLayoutId(IKeyValue item, int postion);
    }

    /**
     * The interface Ws view ret view.
     */
    public  interface IWsViewRetView {
        /**
         * Reset view view.
         *
         * @param position    the position
         * @param convertView the convert view
         * @param parent      the parent
         * @return the view
         */
        View resetView(int position, View convertView, IKeyValue data, ViewGroup parent);
    }

    /**
     * The Layoutfun.
     */
    IWsViewGetLayoutId layoutfun = null;
    /**
     * The Layoutkeyall.
     */
    int[] layoutkeyall = null;

    /**
     * The Layoutkeyall e.
     */
    int[] layoutkeyall_e = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    /**
     * The M data.
     */
    List<IKeyValue> mData = new ArrayList<>();
    private IWsViewRetView resetViewListener = null;

    /**
     * Shuzu.
     *
     * @param context the context
     */
    public void shuzu(Context context) {
        layoutkeyall = new int[]{

                WsViewTools.getResource(context, "tag_0", "id"),
                WsViewTools.getResource(context, "tag_1", "id"),
                WsViewTools.getResource(context, "tag_2", "id"),
                WsViewTools.getResource(context, "tag_3", "id"),
                WsViewTools.getResource(context, "tag_4", "id"),
                WsViewTools.getResource(context, "tag_5", "id"),
                WsViewTools.getResource(context, "tag_6", "id"),
                WsViewTools.getResource(context, "tag_7", "id"),
                WsViewTools.getResource(context, "tag_8", "id"),
                WsViewTools.getResource(context, "tag_9", "id"),
                WsViewTools.getResource(context, "tag_10", "id")
        };

    }

    /**
     * Instantiates a new Ws view adapter.
     *
     * @param context the context
     * @param id      the id
     * @param data    the data
     */
    public WsViewAdapter(Context context, int id, List<IKeyValue> data) {
        shuzu(context);
        this.mInflater = LayoutInflater.from(context);
        lay_out_id = id;
        this.mData = data;
        if(this.mData==null)
        {
            this.mData = new ArrayList<>();
        }
        this.context = context;
        clickListener = new WsVIewClickListener(context);

    }

    /**
     * Instantiates a new Ws view adapter.
     *
     * @param context the context
     * @param data    the data
     * @param l       the l
     */
    public WsViewAdapter(Context context, List<IKeyValue> data, IWsViewGetLayoutId l) {
        shuzu(context);
        this.mInflater = LayoutInflater.from(context);

        this.mData = data;
        if(this.mData==null)
        {
            this.mData = new ArrayList<>();
        }
        this.context = context;
        this.layoutfun = l;
        clickListener = new WsVIewClickListener(context);
    }


    /**
     * Sets reset view listener.
     *
     * @param l the l
     */
    public void setResetViewListener(IWsViewRetView l) {
        this.resetViewListener = l;
    }

    private int[] getLayoutId(IKeyValue item, int postion) {
        if (lay_out_id != 0) {
            return new int[]{lay_out_id, layoutkeyall[0]};
        } else if (layoutfun != null) {
            int id = layoutfun.getLayoutId(item, postion);
            return new int[]{id, getRidbyId(id)};
        }
        return null;
    }

    private int getRidbyId(int id) {
        int index = 0;

        for (int i = 0; i < layoutkeyall_e.length; i++) {
            if (layoutkeyall_e[i] == id) {
                return layoutkeyall[i];
            }
            if (layoutkeyall_e[i] == 0) {
                index = i;
                break;
            }

        }
        layoutkeyall_e[index] = id;
        return layoutkeyall[index];


    }


    /**
     * Add item.
     *
     * @param item the item
     */
    public void addItem(IKeyValue item) {
        mData.add(item);
    }

    /**
     * Add item more.
     *
     * @param items the items
     */
    public void addItemMore(ArrayList<IKeyValue> items) {

        for (IKeyValue item : items) {
            mData.add(item);
        }

    }

    /**
     * Init data.
     *
     * @param items the items
     */
    public void initData(ArrayList<IKeyValue> items) {
        mData.clear();
//        mData = new ArrayList<>();
        for (IKeyValue item : items) {
            mData.add(item);
        }

    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public IKeyValue getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


        int[] tags = getLayoutId(getItem(position), position);
        if (convertView == null || convertView.getTag(tags[1]) == null) {

            //viewManager.clearAllItemView();
            convertView = mInflater.inflate(tags[0], null);

            convertView.setTag(tags[1], WsViewTools.getWsViews(convertView));

        }
        for (IWsView item : (ArrayList<IWsView>) convertView.getTag(tags[1]))
            item.bindData(getItem(position), clickListener);


        if (this.resetViewListener != null) {
            convertView = this.resetViewListener.resetView(position, convertView,getItem(position), parent);
        }

        return convertView;
    }
}
